<?php
class ProfilesController extends AppController {

	var $name = 'Profiles';
	var $uses = array('User', 'Icon', 'Profile');
	var $scaffold;
	var $components = array('AdminAuth', 'Pmaimai', 'RequestHandler');
	var $protectedActions = array('index', 'delete', 'view');
	
	var $helpers = array('Html', 'Form');
	
	
	function changenickname()
	{
		$this->set('error', false);
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->set('user', $user);
		
		if(!isset($this->data['Profile'])){
			return;
		}
		
		$this->User->set($user);
		
		if(empty($this->data['Profile']['nickname'])){
			$this->User->invalidate('existempty', 'ニックネームが未入力です');
		}
		
		if(!$this->User->changeNickname($this->data['Profile']['nickname'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->User->validationErrors);
			return;
		}
		$this->flash("ニックネームを変更しました", array('controller' => 'members', 'action' => 'profile'));
	}
	
	function edit($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->Profile->unbindFully();
		$profile = $this->Profile->findByIdAndStatus($id, 'active');
		if(!$profile || $profile['Profile']['user_id'] !== $userID){
			$this->flash("編集できません", array('controller' => 'members', 'action' => 'profile'));
			return;
		}
		$this->set('profile', $profile);
		$this->Profile->set($profile);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->set('user', $user);
		
		if(!isset($this->data['Profile'])){
			return;
		}
		
		if(empty($this->data['Profile']['title']) || empty($this->data['Profile']['content']) || empty($this->data['Profile']['control']) ){
			$this->Profile->invalidate('empty', '未入力箇所があります');
		}
		if(!$this->Profile->modify($this->data['Profile']['title'], $this->data['Profile']['content'], $this->data['Profile']['control'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->Profile->validationErrors);
			return;
		}
		$this->flash("プロフィールを変更しました", array('controller' => 'members', 'action' => 'profile'));
		
	}
	function eliminate($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->Profile->unbindFully();
		$profile = $this->Profile->findByIdAndStatus($id, 'active');
		if(!$profile || $profile['Profile']['user_id'] !== $userID){
			$this->flash("編集できません", array('controller' => 'members', 'action' => 'profile'));
			return;
		}
		$this->set('profile', $profile);
		$this->Profile->set($profile);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->set('user', $user);
		
		if(!isset($this->data['Profile'])){
			return;
		}
		
		if(empty($this->data['Profile']['agree']) || $this->data['Profile']['agree'] !== '1'){
			$this->Profile->invalidate('agree', '削除しますにチェックを入れてください');
		}
		
		if(!$this->Profile->updateStatus('disactivate')){
			$this->set('error', true);
			$this->set('errorMsg', $this->Profile->validationErrors);
			return;
		}
		$this->flash("プロフィールを削除しました", array('controller' => 'members', 'action' => 'profile'));
	}
	
	
	function add()
	{
		$this->set('error', false);
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->User->set($user);
		$this->set('user', $user);
		
		if(!isset($this->data['Profile'])){
			return;
		}
		
		if(empty($this->data['Profile']['title']) || empty($this->data['Profile']['content']) || empty($this->data['Profile']['control']) ){
			$this->Profile->invalidate('empty', '未入力箇所があります');
		}
		
		if(!$this->Profile->register($userID, $this->data['Profile']['title'], $this->data['Profile']['content'], $this->data['Profile']['control'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->Profile->validationErrors);
			return;
		}
		$this->flash("プロフィールを追加しました", array('controller' => 'members', 'action' => 'profile'));
	}
	
	function changecontrol()
	{
		$this->set('error', false);
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->User->set($user);
		$this->set('user', $user);
		
		if(!isset($this->data['Profile'])){
			return;
		}
		
		
		if(empty($this->data['Profile']['control'])){
			$this->User->invalidate('range', '公開範囲が未選択です');
		}
		
		if(!$this->User->changeProfileControl($this->data['Profile']['control'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->User->validationErrors);
			return;
		}
		$this->flash("プロフィールのデフォルト公開範囲を変更しました", array('controller' => 'members', 'action' => 'profile'));

	}
	
	function changeicon()
	{
		$this->set('error', false);
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->User->set($user);
		$this->set('user', $user);
		
		$icons = $this->Icon->findAll(null, array('id', 'name', 'ext'));
		if(!$icons){
			$this->redirect('/');
		}
		$this->set('icons', $icons);
		
		if(!isset($this->data['Profile'])){
			return;
		}
		
		
		if(empty($this->data['Profile']['iconid'])){
			$this->User->invalidate('range', 'アイコンが未選択です');
		}
		
		if(!$this->User->changeIcon($this->data['Profile']['iconid'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->User->validationErrors);
			return;
		}
		
		$this->flash("アイコンを変更しました", array('controller' => 'members', 'action' => 'profile'));
		
	
	}

}
?>